<template>
    <div class="container">
      <div class="return-page" @click="$router.push('/')">
        <img src="../assets/images/return.svg" alt="">
      </div>
      <div class="login-container d-flex align-items-center justify-content-center vh-100">
        <div class="row w-100">
          <div class="col-md-6 d-md-block">
            <img src="../assets/images/login_bg.webp" alt="Login Background" class="img-fluid">
          </div>
          <div class="col-md-6 d-flex align-items-center justify-content-center">
            <el-card class="box-card shadow-lg">
              <div slot="header" class="text-center">
                <span>欢迎注册</span>
              </div>
              <el-form :model="registerForm" :rules="rules" ref="registerForm" label-width="0px" class="demo-ruleForm">
                <!-- 手机号码 -->
                <el-form-item prop="phone" style="width: 100%;">
                  <el-input v-model="registerForm.phone" placeholder="请输入手机号码"></el-input>
                </el-form-item>
  
                <!-- 短信验证码 -->
                <el-form-item prop="verificationCode" style="width: 100%;">
                  <el-row :gutter="20">
                    <el-col :span="16">
                      <el-input v-model="registerForm.verificationCode" placeholder="请输入短信验证码"></el-input>
                    </el-col>
                    <el-col :span="8">
                      <el-button class="send_code_bt" type="primary" @click="sendVerificationCode" :disabled="isSendingCode">{{ sendButtonText }}</el-button>
                    </el-col>
                  </el-row>
                </el-form-item>
  
                <!-- 密码 -->
                <el-form-item prop="password" style="width: 100%;">
                  <el-input type="password" v-model="registerForm.password" placeholder="请输入密码"></el-input>
                </el-form-item>
  
                <!-- 再次输入密码 -->
                <el-form-item prop="confirmPassword" style="width: 100%;">
                  <el-input type="password" v-model="registerForm.confirmPassword" placeholder="请再次输入密码"></el-input>
                </el-form-item>
  
                <!-- 按钮组 -->
                <el-form-item>
                  <el-button type="primary" @click="submitForm('registerForm')" class="login_bt">注册</el-button>
                  &nbsp; &nbsp;
                  <router-link to="/login" class="">
                    <el-button type="success" class="register_bt">已有账号？去登录</el-button>
                  </router-link>
                </el-form-item>
              </el-form>
            </el-card>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script>
  export default {
    name: 'Register',
    data() {
      const validateConfirmPassword = (rule, value, callback) => {
        if (value !== this.registerForm.password) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      };
      return {
        registerForm: {
          phone: '',
          verificationCode: '',
          password: '',
          confirmPassword: ''
        },
        rules: {
          phone: [
            { required: true, message: '请输入手机号码', trigger: 'blur' },
            { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码格式', trigger: 'blur' }
          ],
          verificationCode: [
            { required: true, message: '请输入短信验证码', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
          ],
          confirmPassword: [
            { required: true, message: '请再次输入密码', trigger: 'blur' },
            { validator: validateConfirmPassword, trigger: 'blur' }
          ]
        },
        isSendingCode: false,
        sendButtonText: '发送验证码'
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 这里添加你的注册逻辑
            console.log('Phone:', this.registerForm.phone);
            console.log('Verification Code:', this.registerForm.verificationCode);
            console.log('Password:', this.registerForm.password);
            console.log('Confirm Password:', this.registerForm.confirmPassword);
            alert('注册成功!');
          } else {
            console.log('表单验证失败');
            return false;
          }
        });
      },
      sendVerificationCode() {
        if (this.registerForm.phone) {
          this.isSendingCode = true;
          this.sendButtonText = '发送中...';
          setTimeout(() => {
            this.sendButtonText = '已发送';
            setTimeout(() => {
              this.isSendingCode = false;
              this.sendButtonText = '重新发送';
            }, 60000); // 60秒后可以重新发送
          }, 2000); // 模拟发送验证码的延迟
        } else {
          this.$message.error('请输入手机号码');
        }
      }
    }
  };
  </script>
  
  <style scoped>
  a {
    text-decoration: none;
  }
  .return-page {
    position: fixed;
    top: 30px;
    cursor: pointer;
  }
  .text-center {
    font-size: 25px;
    font-weight: normal;
    font-style: normal;
    color: #000;
  }
  .send_code_bt{
    background: rgba(255, 144, 3);
    border: 0;
  }
  .el-card {
    width: 500px;
    background-color: rgba(247, 247, 247, 0.9);
    padding: 50px;
    box-shadow: 0px 0px 20px #dddddd;
    border-radius: 5px;
  }
  .login_bt {
    background: rgba(255, 144, 3);
    border: 0;
  }
  .register_bt {
    background: transparent;
    border: 1px solid rgba(255, 144, 3);
    color: rgba(255, 144, 3);
  }
  .forget_pw {
    color: #999;
  }
  .forget_pw:hover {
    color: #333;
  }
  </style>